<?php 
session_start(); 
if(!isset($_SESSION['IsLogin']))
{
	die("Thank you for your using this application! </br> To reaccess this page, you need to <a href='index.php'>LOGIN</a>"); // Make sure they are logged in!
}
else
{
	echo "<h3>Hello ".$_SESSION['IsLogin']."!</h3>";
	print "<form action ='invoice.php' method='post'>";
	print '<div class="cart_title"></div>';
	print  "<input type= 'submit' name= 'logout' value= 'Log out'>";
	if(isset($_POST['logout']))
	{
		session_destroy();
		Header("Location: invoice.php");
		
	}
	
	print "</form>";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Shop</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="css/iecss.css" />
<![endif]-->
<script type="text/javascript" src="js/boxOver.js"></script>
<script type="text/javascript">  

</script>
</head>
<body>
<div id="main_content"> 
   
            <div id="menu_tab">
            <div class="left_menu_corner"></div>
                    <ul class="menu">
                         <li><a href="index.php" class="nav1">  Home </a></li>
                         <li class="divider"></li>
                    </ul>

             <div class="right_menu_corner"></div>
            </div><!-- end of menu tab -->

<?php
//Bring product arrays
include 'product_data.inc';

//define functions
require_once 'functions.inc';
$all_users=  arrayfile_to_array('./user_data.dat');


//invoice table ?>
<?php 
//security
if(isset($_SESSION['IsLogin'])){
print "This is your shopping cart, ".$_SESSION['IsLogin']. "!";
print "<br>";
print "<font size= 5><b>INVOICE</b>";
print "<br>";
print "<table border= '1' cellpadding= '5' cellspacing= '0'>";
print '<th>Name</th> <th>Quantity Ordered</th> <th>Price Per Item</th> <th>Extended Price</th> <th>Edit</th>';

//This starts the invoice total as $0 and then adds to the total.
//echo "<form action ='invoice.php' method='post'>"; 
//print "<input type=hidden name=username value=$username>"; 
$total = 0;
$qty_ordered= 0;
$subtotal=0;


for ($i= 0; $i < count($products); $i++) { 
	if(isset($_POST["quantity$i"]))
		@$_SESSION["quantity$i"] = $_POST["quantity$i"];
	
	$qty= @$_SESSION["quantity$i"];
	
	// This checks to see if the user inputted an integer.
	if ($qty> 0 && $qty- (int) $qty== 0) {
		if(array_key_exists('delete$i',$_POST)){
			$qty = $qty-1;
			Header("Location: invoice.php");
		}
	        
		// This calculates the price if the user orders more than 1 item.
		$extended_price = $qty*$products[$i]['Price'];
	
		// This calculates the total price before shipping and tax.
		$total = $total+$extended_price;
		
		// This displays the data I want in a table.
		print "<tr><td colspan= '1'>{$products[$i]['Name']}</td><td colspan= '1'>$qty</td>";
			print "<td colspan= '1'>";
				printf('$%.2f',"{$products[$i]['Price']}");
			print "</td>";
		print "<td colspan= '1'>";
				printf('$%.2f',($extended_price));
			print "</td>";
		print "<td colspan= '1'>";
				
				print "<input type='submit' name='delete$i' value='Delete'>";
				
			print "</td></tr>";
		print "</table";    
    }
}


// Here, I want to calculate extra costs such as taxes.
// The tax in Hawaii is 4.72%.
$tax= .0472;
$tax_amount= $tax*$total;

	print "<tr><td colspan= '3'><b>Subtotal</b></td>";
	print "<td colspan= '1'>";
            //This is the correct format of prices.
            printf('$%.2f', $total);
        print "</td>";
	print "<tr><td colspan= '3'><b>Tax</b></td>";
	print "<td colspan= '1'>";
            printf('$%.2f',($tax_amount));
	print "</td>";
	print "<tr><td colspan='3'><b>Shipping</b></td>";


// I included different shipping rates.
// I believe that shipping costs influence buyers.
$shipping = 0;
if($total > 0) $shipping = 10;
if($total > 100) $shipping = 25;
if($total > 200) $shipping = 0;
	print "<td colspan= '1'>";
            printf('$%.2f',$shipping);
        print "</td></tr>";


$subtotal= $total+$tax_amount+$shipping;

	print "<tr><td colspan='3'><b>Grand Total</b></td>";
	print "<td colspan= '1'>";
            printf('$%.2f',$subtotal);
        print "</td></tr>";
	$_SESSION["total"] = $subtotal; 	
	print "<form action ='buy.php' method='post'>"; 
	print "<input type=hidden name=total value=$subtotal>";	
	print 	"<input type='submit' name='creditcard' value='Credit Card'></br>";
	print 	"<input type='submit' name='moneyorder' value='Money Order'>";
	
	print "</form>";
}
 
?>